package com.example.sunmiv1.activity.member;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.MemberLabelManagerAdapter;
import com.example.sunmiv1.base.BaseActivity;
import com.example.sunmiv1.dialog.AddMemberLabelDialog;
import com.example.sunmiv1.entity.member.MemberLabelManagerInfo;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VipLabelManagerActivity extends BaseActivity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.vipLabelManager_search)
    ImageView vipLabelManagerSearch;
    @BindView(R.id.vipLabelManager_edit)
    EditText vipLabelManagerEdit;
    @BindView(R.id.vipLabelManager_add)
    ImageView vipLabelManagerAdd;
    @BindView(R.id.vipLabelManager_list)
    PullToRefreshListView vipLabelManagerList;

    private int page = 1;
    private Context context;
    private LoadingDialog loadingDialog;

    private MemberLabelManagerInfo memberLabelManagerInfo;
    private MemberLabelManagerAdapter memberLabelManagerAdapter;
    private List<MemberLabelManagerInfo.DataBean.ListBean> listBeanList = new ArrayList<>();
    private ACache aCache;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    page++;
                    listBeanList.addAll(memberLabelManagerInfo.getData().getList());
                    setMemberLabelManagerAdapter();
                    break;
                case 2:
                    setMemberLabelManagerAdapter();
                    break;
            }
        }
    };

    @Override
    protected void initView() {
        setContentView(R.layout.activity_vip_label_manager);
        ButterKnife.bind(this);
        titleTitle.setText("會員標籤");
        context = this;
        loadingDialog = new LoadingDialog(context);
    }

    @Override
    protected void initData() {

        aCache=ACache.get(this);
        getLabelList();
    }

    @Override
    protected void initListener() {
        vipLabelManagerList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                listBeanList.clear();
                setMemberLabelManagerAdapter();
                getLabelList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getLabelList();
            }
        });
    }

    @OnClick({R.id.title_back, R.id.vipLabelManager_search, R.id.vipLabelManager_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.vipLabelManager_search:
                page = 1;
                listBeanList.clear();
                setMemberLabelManagerAdapter();
                getLabelList();
                break;
            case R.id.vipLabelManager_add:
                AddMemberLabelDialog.newInstance("")
                        .startShow(getSupportFragmentManager(), "")
                        .callback(o -> {
                            page = 1;
                            listBeanList.clear();
                            setMemberLabelManagerAdapter();
                            getLabelList();
                        }).setCancelable(false);
                break;
        }
    }


    private void setMemberLabelManagerAdapter() {
        if (memberLabelManagerAdapter == null) {
            memberLabelManagerAdapter = new MemberLabelManagerAdapter(context,getSupportFragmentManager(),listBeanList);
            vipLabelManagerList.setAdapter(memberLabelManagerAdapter);
        } else {
            memberLabelManagerAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 獲取會員標籤列表
     */
    private void getLabelList() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("cashier_id",aCache.getAsString("uid"));
            jsonObject.put("name", vipLabelManagerEdit.getText().toString().trim());
            jsonObject.put("page", page);
            jsonObject.put("pageSize", 20);
            jsonObject = Tools.createParameterCode(jsonObject);
            HttpUtils.post(context, Common.GET_VIP_LABEL, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    vipLabelManagerList.onRefreshComplete();
                    memberLabelManagerInfo = GsonUtils.JsonToBean(text, MemberLabelManagerInfo.class);
                    Message msg = new Message();
                    if (memberLabelManagerInfo.getStatus() == 1) {
                        msg.what = 1;
                    } else {
                        msg.what = 2;
                    }
                    handler.sendMessage(msg);
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                    vipLabelManagerList.onRefreshComplete();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
